package com.xdq.eduorder.controller;


import com.xdq.commonutils.R;
import com.xdq.eduorder.entity.PayLog;
import com.xdq.eduorder.service.PayLogService;
import com.xdq.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-09-27
 */
@RestController
@CrossOrigin
@RequestMapping("/eduorder/paylog")
public class PayLogController {

    @Autowired
    private PayLogService payLogService;

    @GetMapping("createNative/{orderNo}")
    public R createNative(@PathVariable String orderNo) {
        try {
            Map map = payLogService.createNative(orderNo);
            System.out.println("返回二维码 map 集合：" + map);
            return R.ok().data(map);
        } catch (GuliException e) {
            return R.error().message(e.getMessage());
        }
    }

    // 订单支付状态
    @GetMapping("queryPayStatus/{orderNo}")
    public R queryPayStatus(@PathVariable String orderNo) {
        Map<String, String> map = payLogService.queryPayStatus(orderNo);
        System.out.println("返回支付状态 map 集合：" + map);
        if (map == null) {
            return R.error().message("支付出错了");
        }
        if (map.get("trade_state").equals("SUCCESS")) {
            payLogService.updateOrderStatus(map);
            return R.ok();
        }
        return R.ok().code(25000).message("支付中");
    }

}

